package org.cwnuit.course.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.cwnuit.course.dao.AccountDAO;
import org.cwnuit.course.dao.ClassgroupDAO;
import org.cwnuit.course.dao.CourseDAO;
import org.cwnuit.course.domain.Account;
import org.cwnuit.course.domain.Classgroup;
import org.cwnuit.course.domain.Course;

/**
 * 用户信息Service
 * @author john
 */
public class AccountService {
	
	
	private AccountDAO accountDAO = new AccountDAO();
	private ClassgroupDAO classgroupDAO =  new ClassgroupDAO();
	private CourseDAO courseDAO =  new CourseDAO();
	
	/**
	 *用户登录
	 * @return
	 */
	public Account loginAccount(String username){
		return accountDAO.loginAccount(username);
	}
	
	/**
	 * 获取用所有户信息
	 * @return
	 */
	public List<Account> listAccount(){
		return accountDAO.listAccount();
	}
	
	/**
	 * 通过用户ID获取用户信息
	 * @param userid
	 */
	public Account getAccount(Integer userid){
		return accountDAO.getAccount(userid);
	}
	

	/**
	 * 添加用户信息
	 * @param account
	 */
	public boolean saveAccount(HttpServletRequest request) {
		String useridStr = request.getParameter("userid")==""?"0":request.getParameter("userid");
		int userid = Integer.parseInt(useridStr);
		int objectid = Integer.parseInt(request.getParameter("objectid"));
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Integer groupid = Integer.parseInt(request.getParameter("groupid"));
		Classgroup classgroup = classgroupDAO.getClassgroup(groupid);
		String groupname = classgroup.getGroupname();
		String realname = request.getParameter("realname");
		int usersex = Integer.parseInt(request.getParameter("usersex"));
		String userpic = request.getParameter("userpic");
		String userphone = request.getParameter("userphone");
		Course course = courseDAO.getCourse(objectid);
		String coursename = course.getObjectname();
		int isonline = Integer.parseInt(request.getParameter("isonline"));
		int  userrole = Integer.parseInt(request.getParameter("userrole"));
		String remarks = request.getParameter("remarks");
		Account account = new Account();
		account.setUserid(userid);
		account.setObjectid(objectid);
		account.setUsername(username);
		account.setPassword(password);
		account.setGroupname(groupname);
		account.setGroupid(groupid);
		account.setRealname(realname);
		account.setUsersex(usersex);
		account.setUserpic(userpic);
		account.setCoursename(coursename);
		account.setUserphone(userphone);
		account.setIsonline(isonline);
		account.setUserrole(userrole);
		account.setRemarks(remarks);
		try{
			if(account.getUserid()!=0){
				accountDAO.updateAccount(account);
			}else{
				accountDAO.insertAccount(account);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 用户修改个人信息
	 * @param account
	 */
	public boolean saveUserset(HttpServletRequest request) {
		String useridStr = request.getParameter("userid")==""?"0":request.getParameter("userid");
		int userid = Integer.parseInt(useridStr);
		int objectid = Integer.parseInt(request.getParameter("objectid"));
		Integer groupid = Integer.parseInt(request.getParameter("groupid"));
		Classgroup classgroup = classgroupDAO.getClassgroup(groupid);
		String groupname = classgroup.getGroupname();
		String realname = request.getParameter("realname");
		int usersex = Integer.parseInt(request.getParameter("usersex"));
		String userpic = request.getParameter("userpic");
		String userphone = request.getParameter("userphone");
		Course course = courseDAO.getCourse(objectid);
		String coursename = course.getObjectname();
		String remarks = request.getParameter("remarks");
		Account account = new Account();
		account.setUserid(userid);
		account.setObjectid(objectid);
		account.setGroupname(groupname);
		account.setGroupid(groupid);
		account.setRealname(realname);
		account.setUsersex(usersex);
		account.setUserpic(userpic);
		account.setCoursename(coursename);
		account.setUserphone(userphone);
		account.setRemarks(remarks);
		try{
			accountDAO.saveUserset(account);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除取用户信息
	 * @param userid
	 */
	public boolean deleteAccount(Integer userid){
		try{
			Account account = new Account();
			account.setUserid(userid);
			accountDAO.deleteAccount(account);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}

}
